package com.eleme.order.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.eleme.order.dto.OrderCreateDTO;
import com.eleme.order.dto.OrderDetailDTO;
import com.eleme.order.entity.Order;
import com.eleme.order.entity.OrderDetail;
import com.fusion.eleme.order.mapper.OrderDetailMapper;
import com.eleme.order.mapper.OrderMapper;
import com.eleme.order.service.OrderService;
import com.eleme.order.vo.OrderDetailVO;
import com.eleme.order.vo.OrderVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class OrderServiceImpl implements OrderService {
    @Autowired
    private OrderMapper orderMapper;

    @Override
    public Long createOrder(OrderCreateDTO dto) {
        if (CollectionUtils.isEmpty(dto.getDetails())) {
            throw new RuntimeException("订单明细不能为空");
        }
        Order order = new Order();
        order.setUserId(dto.getUserId());
        order.setBusinessId(dto.getBusinessId());
        order.setTotalAmount(dto.getTotalAmount());
        order.setOrderStatus(0);
        order.setCreateTime(new Date());
        orderMapper.insert(order);
        for (OrderDetailDTO d : dto.getDetails()) {
            OrderDetail detail = new OrderDetail();
            BeanUtils.copyProperties(d, detail);
            detail.setOrderId(order.getOrderId());
            orderDetailMapper.insert(detail);
        }
        return order.getOrderId();
    }

    @Override
    public boolean payOrder(Long orderId) {
        Order order = orderMapper.selectById(orderId);
        if (order == null) throw new RuntimeException("订单不存在");
        if (order.getOrderStatus() != 0) throw new RuntimeException("订单状态异常");
        order.setOrderStatus(1);
        order.setPayTime(new Date());
        return orderMapper.updateById(order) > 0;
    }

    @Override
    public boolean cancelOrder(Long orderId) {
        Order order = orderMapper.selectById(orderId);
        if (order == null) throw new RuntimeException("订单不存在");
        if (order.getOrderStatus() != 0) throw new RuntimeException("订单不可取消");
        order.setOrderStatus(2);
        return orderMapper.updateById(order) > 0;
    }

    @Override
    public List<OrderVO> listOrdersByUser(String userId) {
        QueryWrapper<Order> query = new QueryWrapper<>();
        query.eq("user_id", userId);
        List<Order> orders = orderMapper.selectList(query);
        List<OrderVO> res = new ArrayList<>();
        for (Order o : orders) {
            res.add(toVO(o));
        }
        return res;
    }

    @Override
    public OrderVO getOrderDetail(Long orderId) {
        Order order = orderMapper.selectById(orderId);
        if (order == null) throw new RuntimeException("订单不存在");
        return toVO(order);
    }

    private OrderVO toVO(Order order) {
        OrderVO vo = new OrderVO();
        BeanUtils.copyProperties(order, vo);
        QueryWrapper<OrderDetail> query = new QueryWrapper<>();
        query.eq("order_id", order.getOrderId());
        List<OrderDetail> details = orderDetailMapper.selectList(query);
        List<OrderDetailVO> detailVOList = new ArrayList<>();
        for (OrderDetail d : details) {
            OrderDetailVO dvo = new OrderDetailVO();
            BeanUtils.copyProperties(d, dvo);
            detailVOList.add(dvo);
        }
        vo.setDetails(detailVOList);
        return vo;
    }
} 